package com.smartsofa.photo;


import com.smartsofa.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class GridClassify extends PopupWindow {

	private Activity mContext;
	private View mMenuView;
	private ViewFlipper viewfipper;
	private Button btn_submit, btn_cancel;
	private GridView wheel2;
	public int positionid;
	public int whichcir=0;
	public int arg;
	public int doc=0;
	private String[] category_str1 =null;
	private String[] same_circle =null;
	private String[] hos_circle =null;
	private Spinner same;
	public TextView hos;
	private ArrayAdapter<String> sameadapter;
	private String[][] category2 = null;
	/**
	 * @param context
	 * @param classify
	 * @param itemsOnClick
	 */
	public GridClassify(Activity context,String[] classify,String[] samecircle,String[] hoscircle,String[][] doctors, OnClickListener itemsOnClick) {
		super(context);
		mContext = context;
		category_str1=classify;
		same_circle=samecircle;
		hos_circle=new String[hoscircle.length-1];
		for(int i=0;i<hoscircle.length-1;i++){
			hos_circle[i]=hoscircle[i+1];
		}
		category2=doctors;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mMenuView = inflater.inflate(R.layout.classify_pop1, null);
		viewfipper = new ViewFlipper(context);
		viewfipper.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));

		wheel2 = (GridView) mMenuView.findViewById(R.id.gridView1);
		btn_submit = (Button) mMenuView.findViewById(R.id.submit1);
		btn_cancel = (Button) mMenuView.findViewById(R.id.cancel1);
		
		same = (Spinner) mMenuView.findViewById(R.id.circle1);
		  //����ѡ������ArrayAdapter��������  
        sameadapter = new ArrayAdapter<String>(mContext,android.R.layout.simple_spinner_item,same_circle);         
        //���������б�ķ��  
        sameadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        
        //��adapter ��ӵ�spinner��  
        same.setAdapter(sameadapter);         
        //���Spinner�¼�����    
        same.setOnItemSelectedListener(new SameSelectedListener());          

        
	  hos = (TextView) mMenuView.findViewById(R.id.circle2); 
	  hos.setText(hoscircle[0]);
      //����¼�����    
      hos.setOnClickListener(new OnClickListener() {  
          public void onClick(View v) {  
        	  showSelectDialog(mContext, "ҽԺ-ҽ��", hos_circle, category2); 
          }  
      });          
        
		btn_submit.setOnClickListener(itemsOnClick);
		//ȡ��ť  
        btn_cancel.setOnClickListener(new OnClickListener() {  
            public void onClick(View v) {  
                //��ٵ�����  
                dismiss();  
            }  
        });        
        
        gridadapter adapter = new gridadapter(mContext, category_str1);
        wheel2.setAdapter(adapter);
        wheel2.setSelector(new ColorDrawable(Color.TRANSPARENT));
        wheel2.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
            	positionid=position;
            }
        });         
        
      //mMenuView���OnTouchListener�����жϻ�ȡ����λ�������ѡ�����������ٵ�����
        mMenuView.setOnTouchListener(new OnTouchListener() {  
            
            public boolean onTouch(View v, MotionEvent event) {  
                  
                int height = mMenuView.findViewById(R.id.pop_layout1).getTop();  
                int y=(int) event.getY();  
                if(event.getAction()==MotionEvent.ACTION_UP){  
                    if(y<height){  
                        dismiss();  
                    }  
                }                 
                return true;  
            }  
        }); 
		viewfipper.addView(mMenuView);
		viewfipper.setFlipInterval(6000000);
		this.setContentView(viewfipper);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);
		ColorDrawable dw = new ColorDrawable(0x00000000);
		this.setBackgroundDrawable(dw);
		this.update();

	}

	@Override
	public void showAtLocation(View parent, int gravity, int x, int y) {
		super.showAtLocation(parent, gravity, x, y);
		viewfipper.startFlipping();
	}
	
	   //ʹ��������ʽ����  
    class SameSelectedListener implements OnItemSelectedListener{  
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,  
                long arg3) { 
        	whichcir=1;
        	arg=arg2;
        }  
        public void onNothingSelected(AdapterView<?> arg0) {  
        }  
    }
	   //ʹ��������ʽ����  
    private void showSelectDialog(Context context, String title, final String[] left, final String[][] right) {
        AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.setTitle(title);
        LinearLayout llContent = new LinearLayout(context);
        llContent.setOrientation(LinearLayout.HORIZONTAL);
        final WheelView wheelLeft = new WheelView(context);
        wheelLeft.setVisibleItems(5);
        wheelLeft.setCyclic(false);
        wheelLeft.setAdapter(new ArrayWheelAdapter<String>(left));
        final WheelView wheelRight = new WheelView(context);
        wheelRight.setVisibleItems(5);
        wheelRight.setCyclic(false);
        wheelRight.setAdapter(new ArrayWheelAdapter<String>(right[0]));
        LinearLayout.LayoutParams paramsLeft = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        paramsLeft.gravity = Gravity.LEFT;
        LinearLayout.LayoutParams paramsRight = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT, (float) 0.7);
        paramsRight.gravity = Gravity.RIGHT;
        llContent.addView(wheelLeft, paramsLeft);
        llContent.addView(wheelRight, paramsRight);
        wheelLeft.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                wheelRight.setAdapter(new ArrayWheelAdapter<String>(right[newValue]));
                wheelRight.setCurrentItem(right[newValue].length / 2);
            }
        });
        dialog.setButton(AlertDialog.BUTTON_POSITIVE, "ȷ��", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                int leftPosition = wheelLeft.getCurrentItem();
                String vLeft = left[leftPosition];
                String vRight = right[leftPosition][wheelRight.getCurrentItem()];
                int bef=0;
                hos.setText(vLeft + "-" + vRight);
                whichcir=2;
            	arg=leftPosition+1;
            	for(int i=0;i<leftPosition;i++){
            		bef+=right[i].length;
            	}
            	doc=wheelRight.getCurrentItem()+bef+1;
            	//System.out.println("doc..........."+doc);
                dialog.dismiss();
            }
        });
        dialog.setButton(AlertDialog.BUTTON_NEGATIVE, "ȡ��", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog.setView(llContent);
        if (!dialog.isShowing()) {
            dialog.show();
        }
    }
}
